<?php
// +----------------------------------------------------------------------
// +----------------------------------------------------------------------
defined('IN_WZ') or exit('No direct script access allowed');
/**
 * tags模块前台页面
 */
class index {
    private $siteconfigs;
	public function __construct() 
	{

	}

    /**
     * tags首页
     */
    public function init()
	{
        echo "myziduan \n";
	}


    public function show() 
	{

		echo "show myziduan";
        
    }
	
	public function deletedata()
	{
		$mydb = load_class("mydb","api");

		$table = $GLOBALS['table'];
		$where["id"] = $GLOBALS['id'];
		$where['tel'] = $GLOBALS['tel'];


		if(md5($GLOBALS['tel']."123")!=$GLOBALS['code']){
			exit("2");
		}

		if($mydb->delete($table,$where)){
			exit("1");
		}else{
			exit("0");
		}

	}


	
	
	
	public function addnewdata()
	{

		$mydb = load_class("mydb","api");

		$table = $GLOBALS['table'];
		$d['dbname'] = $table ;
		$cums = $mydb->begin()->where($d)->getall("ziduanx");
		$dbs = $mydb->begin()->where($d)->getall("ziduan");
		$subject = "自定义字段【".$dbs[0]["discribe"]."】有新的信息";
		$tomail = $dbs[0]["targetmail"];
		//echo $subject ;
		$body = "";
		foreach ($cums as $r)
		{
			$body .= $r['dbdescribe']."(".  $r['dbcum'].")=>".$GLOBALS[$r['dbcum']]."<br>";
			$c[$r['dbcum']]= $GLOBALS[$r['dbcum']];
		}
		$c['createtime'] = date('Y-m-d H:i:s',time());
        $uid = get_cookie('_uid');
        if($uid){
            $c['uid']=$uid;
        }
		//echo $body ;
        if("yuyue"==$table){
            $c['uid'] = $this->getloginuid();
            if(isset($GLOBALS["uid"])){
                $c['uid'] = $GLOBALS["uid"];
            }
        }
		$mydb->add($table,$c);

		if($dbs[0]["tixing"]==1)
		{
			$mail = load_class("Mail","api");

			$mailarr = explode(";", $tomail);

			foreach ($mailarr as $mails) {
				$mail->send($mails,$subject,$body);
			}
		}
		$re['status'] =1 ;
		$re['msg'] = "ok";
		echo json_encode($re);




	}

	//private $member ;
	public function listing(){




		$member = load_class('foreground',  "member");
		$c["tel"] = $member->memberinfo['mobile'];
		$mydb = load_class("mydb","api");
		$result = $mydb->begin()->where($c)->getall("yuyue");

		include T('myziduan','yuyue');
	}


    public function gethotsearch(){
        $db = load_class("mydb","api");
        $result = $db->where(" blockid=68")->getall("block");
        $arr = explode(",",$result[0]['code']);
        echo  json_encode($arr);

    }

    public function searchpe(){
        //echo $GLOBALS['key'];
        $db = load_class("mydb","api");
        $result = $db->where("status=9 and title like '%".$GLOBALS['key']."%'")->page(1,5)->order("addtime desc")->getall("smgq");
        $r = array();
        foreach ($result as $item){
            array_push($r,array("title"=>$item['title'],"id"=>$item['id'],"cid"=>$item['cid'],"thumb"=>$item['thumb']));
        }
        echo  json_encode($r);



    }

    /**
     * 获取UID
     */
    public function getloginuid(){
        //echo decode($_COOKIE['ZWl_uid']);
        return (decode($_COOKIE['ZWl_uid']));

    }

    /**
     * 获取TEL
     */
    public function getlogintel(){
        $uid = $this->getloginuid();
        if(!$uid) return -1 ;
        $db =load_class("mydb","api");
        $result = $db->where("uid=".$uid)->getall("member");

        if(strlen( $result[0]['mobile'])<11) return "-1";

        return($result[0]['mobile']);

    }

    /**
     * 获取用户头像
     */
    public function getuserface(){

        $db = load_class("mydb","api");
        $where = "" ;
        $c['uid'] = $GLOBALS['uid'];
        $result = $db->begin()->where($c)->getall("member_detail_data");
        $r['status'] = 0 ;
        $r['msg'] = $result;
        echo  json_encode($r);
    }


    /**
     * 根据用户获取优惠券
     */
    public function getyouhuiquan()
    {
        $db = load_class("mydb","api");
        $c['tel'] = $this->getlogintel();
        $result = $db->where($c)->getall("youhuiquan");
        $result2 = array();
        $result3 = array();

        foreach ($result as $x){
            $end = strtotime($x['enddate']);
            if($x['status']==0 || $end < time()){
                array_push($result2,$x);
            }else{
                array_push($result3,$x);

            }


        }



        $r['status'] = 0 ;
        $r['enabled'] = $result3 ;
        $r['disabled'] = $result2 ;
        echo json_encode($r);

    }



    /**
     * 根据uid获取 收藏列表
     */
    public function getprojectfavorite()
    {
        $db = load_class("mydb","api");
        $uid = $this->getloginuid();
        $c['uid'] = $uid;

        if(!$uid) exit( json_encode(array("msg"=>"no login","status"=>1)));
        $last = $db->begin()->where($c)->getall("myfavorite");

        $arr = array();
        foreach ($last as $x){
            $tem = $db->begin()->where("id=".$x['pid'])->getall("smgq");
            //array_push($arr,$tem[0]);
            array_push($arr,array("content"=>$tem[0],"yuyue"=>$x));

        }
        echo json_encode($arr);

    }


    function deletefavfromid(){
        $db = load_class("mydb","api");
        $c['pid'] = $GLOBALS['id'];
        $uid = $this->getloginuid();
        if(!$uid) echo json_encode(array("msg"=>"no login","status"=>1));
        $c['uid'] = $uid;
        $db->begin()->delete("myfavorite",$c);
        $r['msg'] = "ok";
        $r['status'] = 0 ;
        echo json_encode($r);



    }
    function deleteyuyuefromid(){
        $db = load_class("mydb","api");
        $c['pid'] = $GLOBALS['id'];
        $uid = $this->getloginuid();
        $tel = $this->getlogintel();
        if(!$uid) echo json_encode(array("msg"=>"no login","status"=>1));
        $c['uid'] = $uid;
        $db->begin()->delete("yuyue",$c);
        unset($c);
        $c['pid'] = $GLOBALS['id'];
        $c['tel'] = $tel;
        $db->begin()->delete("yuyue",$c);


        $r['msg'] = "ok".json_encode($c);
        $r['status'] = 0 ;
        echo json_encode($r);



    }



    public function addfavorate(){

        if(!$this->getloginuid()){

            exit("{\"status\":3,\"msg\":\"no login\"}");
        }




        $db = load_class("mydb","api");
        $c['uid'] = $this->getloginuid();
        $c['pid'] = $GLOBALS['pid'];
        $c['cid'] = $GLOBALS['cid'];

        if(count($db->begin()->where($c)->getall("myfavorite"))==0){
            $c['createtime'] = date('Y-m-d H:i:s',time());
            $db->begin()->add("myfavorite",$c);
            $r['status']=0;
            $r['msg'] = "ok";
        }else{
            $r['status']=1;
            $r['msg'] = "重复添加";
        }

        echo  json_encode($r);

    }

    /**
     * 根据uid或者手机号获取项目
     */
    public function findProjectbyuidortel()
    {
        $tel = $this->getlogintel();
        $uid = $this->getloginuid();
        if(!$uid) exit (json_encode(array()));
        $db = load_class("mydb","api");
        $result =$db->where("uid=".$uid ." OR tel=".$tel)->getall("yuyue");
        //dump($result);
        $arr = array();
        foreach ($result as $item)
        {
            if($item['pid']!=null)
            $arr[$item['pid']] = $item ;


        }
        $return = array();
        foreach ($arr as $item=>$value){

            $c['cid'] = $value['cid'];
            $c['id'] = $value['pid'];
            $re = $db->where($c)->getall("smgq");
            array_push($return,array("content"=>$re[0],"yuyue"=>$value));

        }

        echo json_encode($return);

    }

    /**
     * 用户注册API
     */
    public function member_reg(){
        $c['mobile'] = $GLOBALS['mobile'];
        $db = load_class("mydb","api");
        if(count($db->where($c)->getall("member"))>0){
            exit(json_encode(array("status"=>1,"msg"=>"用户已存在,请通过找回密码取回!")));


        }


        $c['username'] = $GLOBALS['mobile'];
        $c['truename'] = $GLOBALS['mobile'];
        $c['pw_reset'] = 0;
        $c['modelid'] = 10;
        $factor = rand(100000,999999);
        $c['factor'] = $factor;
        $c['password'] = md5(md5($GLOBALS['password']).$factor);
        if($db->add("member",$c)){
            exit(json_encode(array("status"=>0,"msg"=>"ok")));
        }else{
            exit(json_encode(array("status"=>2,"msg"=>"发生错误")));
        }


    }
    /**
     * 用户注册API
     */
    public function member_resetpsd(){
        $c['mobile'] = $GLOBALS['mobile'];
        $db = load_class("mydb","api");
        if(count($db->where($c)->getall("member"))==0){
            exit(json_encode(array("status"=>1,"msg"=>"用户已不存在,请注册!")));


        }


        $d['username'] = $GLOBALS['mobile'];
        $c['pw_reset'] = 0;
        $c['modelid'] = 10;
        $factor = rand(100000,999999);
        $c['factor'] = $factor;
        $c['password'] = md5(md5($GLOBALS['password']).$factor);
        if($db->where($d)->save("member",$c)){
            exit(json_encode(array("status"=>0,"msg"=>"ok")));
        }else{
            exit(json_encode(array("status"=>2,"msg"=>"发生错误")));
        }


    }

    /**
     * 用户登录API
     */
    public function login()
    {
        $c['mobile'] = $GLOBALS['mobile'];

        if(strlen($c["mobile"])<11){
            exit(json_encode(array("status"=>1,"msg"=>"参数错误")));
        }
        $db = load_class("mydb","api");
        $result = $db->where($c)->getall("member");
        if(count($result)>0){
            $factor = $result[0]['factor'];
            //echo $factor;
            $psd = md5(md5($GLOBALS['password']).$factor);
            //echo $psd."    ".$result[0]['password']."   ".$GLOBALS['password'];
            if($psd == $result[0]['password']){
                set_cookie("uid",$result[0]["uid"]);
                set_cookie("uidx",$result[0]["uid"],0,false);
                set_cookie("mobile",$result[0]["mobile"],0,false);
                exit(json_encode(array("status"=>0,"msg"=>"ok")));
            }else{
                exit(json_encode(array("status"=>1,"msg"=>"帐号或密码错误")));
            }

        }else{
            exit(json_encode(array("status"=>1,"msg"=>"帐号或密码错误")));
        }




    }

    function logout(){
        set_cookie("uid",null);
        set_cookie("uidx",null,0,false);
        set_cookie("mobile",null,0,false);
        exit(json_encode(array("status"=>0,"msg"=>"ok")));
    }



}
?>